package pers.mtx.hmw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.util.IntrospectorCleanupListener;
import pers.mtx.hmw.filter.AccesLogFilter;
import pers.mtx.hmw.listener.MyHttpSessionListener;

import java.util.EventListener;

@SpringBootApplication
public class HmwApplication {

    public static void main(String[] args) {
        SpringApplication.run(HmwApplication.class, args);
    }
    @Bean
    public ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean() {
        ServletListenerRegistrationBean<EventListener> srb = new ServletListenerRegistrationBean<EventListener>();
        //防止Spring内存溢出监听器
        srb.setListener(new IntrospectorCleanupListener());
        //request监听器 主要需要配置这个监听器
        srb.setListener(new RequestContextListener());
        srb.setListener(new MyHttpSessionListener());
        return srb;
    }

    @Bean
    public FilterRegistrationBean<AccesLogFilter> registration() {
        //创建filter
        AccesLogFilter accesLogFilter = new AccesLogFilter();
        //注册过滤器
        FilterRegistrationBean<AccesLogFilter> registration = new FilterRegistrationBean<>(accesLogFilter);
        //添加条件
        registration.addUrlPatterns("/users/*");
        registration.setOrder(1);
        return registration;
    }

}
